package com.lanchetech.admin.service.impl;

import com.alibaba.excel.util.CollectionUtils;
import com.lanchetech.admin.service.CatalogService;
import com.lanchetech.bean.response.ResultData;
import com.lanchetech.dao.CatalogMapper;
import com.lanchetech.entity.Catalog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CatalogServiceImpl implements CatalogService {

    @Autowired
    private CatalogMapper catalogMapper;

    @Override
    public ResultData<List<Catalog>> getCatalogList() {
        List<Catalog> catalogs = catalogMapper.findFather();
        if (!CollectionUtils.isEmpty(catalogs)) {
            for (Catalog catalog : catalogs) {
                getChildren(catalog);
            }
        }
        return new ResultData<>(catalogs);
    }

    private void getChildren(Catalog catalog) {
        // 父亲
        List<Catalog> fathers = catalogMapper.getByPid(catalog.getId());
        if (!CollectionUtils.isEmpty(fathers)) {
            catalog.setCatalogs(fathers);
            for (Catalog father : fathers) {
                // 获取儿子
                getChildren(father);
            }
        }
    }
}
